package com.scau.action.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.CancelShoe;
import com.scau.model.business.OrderShoe;
import com.scau.model.business.Shoe;
import com.scau.model.business.ShoeWarehouse;
import com.scau.service.impl.business.CancelShoeService;
import com.scau.service.impl.business.OrderShoeService;
import com.scau.service.impl.business.ShoeService;
import com.scau.service.impl.business.ShoeWarehouseService;
import com.scau.util.PageController;

/**
 * 此类需要修改 commented by yhl
 * @author yhl
 *
 */
@Component
@Scope("prototype")
public class CancelShoeAction extends BaseAction implements ModelDriven<CancelShoe>,
		Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private CancelShoe cancelShoe = null;
	private CancelShoeService cancelShoeService = null;
	private ShoeService shoeService;
	OrderShoeService orderShoeService ;
	ShoeWarehouseService shoeWarehouseService;
	
	
	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		int totalRows = cancelShoeService.getRecordCount(cancelShoe);		
		String URL =getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<CancelShoe> resourceList = cancelShoeService.list(
				new CancelShoe(), this.pager.getPageStartRow(),
				pager.getPageSize(), null,null);
		
		List<String> product_id_list = new ArrayList<String>();// 在页面中显示相应的成品料号
		Shoe shoe = new Shoe();		
		for (CancelShoe cancelShoe : resourceList) {
			shoe.setId(cancelShoe.getShoe_id());
			product_id_list.add(shoeService.get(shoe).getProduct_id());
			}
		
		request.setAttribute("product_id_list", product_id_list);
		this.pager.setData(resourceList);
		
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		OrderShoe orderShoe = new OrderShoe();
		String order_shoe_id = request.getParameter("order_shoe_id");//订单鞋款的id
		orderShoe.setId(Long.parseLong(order_shoe_id));
		orderShoe = orderShoeService.get(orderShoe);
		Shoe shoe = new Shoe();
		ShoeService shoeService = new ShoeService();
		shoe.setId(orderShoe.getShoe_id());
		shoe = shoeService.get(shoe);
		
		//以下计算同一订单内已开料的数量
	    Long[] cutted = new Long[8];
	    //*******这里有问题*************commented by yhl
//		cutted[0] = BaseDao.getSum("size_33", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[1] = BaseDao.getSum("size_34", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[2] = BaseDao.getSum("size_35", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[3] = BaseDao.getSum("size_36", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[4] = BaseDao.getSum("size_37", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[5] = BaseDao.getSum("size_38", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[6] = BaseDao.getSum("size_39", "cutting", "order_shoe_id="+order_shoe_id);
//		cutted[7] = BaseDao.getSum("size_40", "cutting", "order_shoe_id="+order_shoe_id);
		
		request.setAttribute("order_shoe_id", orderShoe.getId());
	    request.setAttribute("cutted", cutted);
		request.setAttribute("shoe_id", shoe.getId());	
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {	
			
			OrderShoe orderShoe = new OrderShoe();
			OrderShoeService orderShoeService = new OrderShoeService();
			
			orderShoe.setId(cancelShoe.getOrder_shoe_id());				
			orderShoe = orderShoeService.get(orderShoe);
									
			ShoeWarehouse shoeWarehouse = new ShoeWarehouse();//对冲单的鞋款进行入库汇总操作
			
			shoeWarehouse.setShoe_id(cancelShoe.getShoe_id());
			shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
			
			cancelShoeService.save(cancelShoe);//保存对象,写入该鞋款冲单入库记录
			if(null == shoeWarehouse){//添加一种新的鞋款入仓库
				shoeWarehouse = new ShoeWarehouse();
				shoeWarehouse.setShoe_id(orderShoe.getShoe_id());// 初始化数据
				shoeWarehouse.setSize_33(0L);
				shoeWarehouse.setSize_34(0L);
				shoeWarehouse.setSize_35(0L);
				shoeWarehouse.setSize_36(0L);
				shoeWarehouse.setSize_37(0L);
				shoeWarehouse.setSize_38(0L);
				shoeWarehouse.setSize_39(0L);
				shoeWarehouse.setSize_40(0L);
				shoeWarehouseService.save(shoeWarehouse);
				shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
				}				
			shoeWarehouse.setSize_33(shoeWarehouse.getSize_33() + cancelShoe.getSize_33());
			shoeWarehouse.setSize_34(shoeWarehouse.getSize_34() + cancelShoe.getSize_34());
			shoeWarehouse.setSize_35(shoeWarehouse.getSize_35() + cancelShoe.getSize_35());
			shoeWarehouse.setSize_36(shoeWarehouse.getSize_36() + cancelShoe.getSize_36());
			shoeWarehouse.setSize_37(shoeWarehouse.getSize_37() + cancelShoe.getSize_37());
			shoeWarehouse.setSize_38(shoeWarehouse.getSize_38() + cancelShoe.getSize_38());
			shoeWarehouse.setSize_39(shoeWarehouse.getSize_39() + cancelShoe.getSize_39());
			shoeWarehouse.setSize_40(shoeWarehouse.getSize_40() + cancelShoe.getSize_40());// 完成入库
			
			shoeWarehouseService.update(shoeWarehouse);//保存对象
			orderShoe.setTransact_status(1L);//更改交易状态
			orderShoeService.save(orderShoe);			
			return "list";
		} catch (BusinessException e) {
	
		request.setAttribute("cancelShoe", cancelShoe);
		request.setAttribute("message", e.getMessage());
		return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			CancelShoe cancelShoe = new CancelShoe();
			//OrderShoe orderShoe = new OrderShoe();
			if (null != id && !("".equals(id))) {
				cancelShoe.setId(Long.parseLong(id));
				cancelShoeService.delete(cancelShoe);
			
			}
		}
		return "list";
	}
	

	public PageController getPager() {
		return pager;
	}


	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}


	public CancelShoe getCancelShoe() {
		return cancelShoe;
	}


	public void setCancelShoe(CancelShoe cancelShoe) {
		this.cancelShoe = cancelShoe;
	}


	public ShoeService getShoeService() {
		return shoeService;
	}

	public OrderShoeService getOrderShoeService() {
		return orderShoeService;
	}

	public ShoeWarehouseService getShoeWarehouseService() {
		return shoeWarehouseService;
	}

	@Resource
	public void setShoeService(ShoeService shoeService) {
		this.shoeService = shoeService;
	}

	@Resource
	public void setOrderShoeService(OrderShoeService orderShoeService) {
		this.orderShoeService = orderShoeService;
	}

	@Resource
	public void setShoeWarehouseService(ShoeWarehouseService shoeWarehouseService) {
		this.shoeWarehouseService = shoeWarehouseService;
	}

	public CancelShoeService getCancelShoeService() {
		return cancelShoeService;
	}

	@Resource
	public void setCancelShoeService(CancelShoeService cancelShoeService) {
		this.cancelShoeService = cancelShoeService;
	}

	@Override
	public CancelShoe getModel() {
		// TODO Auto-generated method stub
		return cancelShoe;
	}

	

}
